package io.netty.example.study.server.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.example.study.common.ResponseMessage;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.util.List;

/**
 * 服务端二次编码器，从 ResponseMessage 到 ByteBuf；
 * MessageToMessageEncoder<ResponseMessage> 中的 ResponseMessage 是 Input；
 */
public class OrderProtocolEncoder extends MessageToMessageEncoder<ResponseMessage> {

    protected void encode(ChannelHandlerContext ctx, ResponseMessage responseMessage, List<Object> out)
            throws Exception {

        ByteBuf byteBuf = ctx.alloc().buffer();
        responseMessage.encode(byteBuf);

        out.add(byteBuf);
    }

}
